/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean;

import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import edu.doeacc.tms.domain.Driver;
import edu.doeacc.tms.service.dao.DriverFacade;
import edu.doeacc.tms.web.util.JsfUtil;

/**
 *
 * @author Niraj
 */
@ManagedBean(name = "driverBean")
@ViewScoped
public class DriverBean {

    @EJB
    private DriverFacade driverFacade;
    private List<Driver> drivers;
    private Driver current;
    @ManagedProperty("#{msgs}")
    ResourceBundle bundle;

    public void setBundle(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    public DriverFacade getDriverFacade() {
        return driverFacade;
    }

    public void setDriverFacade(DriverFacade driverFacade) {
        this.driverFacade = driverFacade;
    }

    public List<Driver> getDrivers() {
        if (this.drivers == null) {
            this.drivers = driverFacade.findAll();
        }
        return drivers;
    }

    public void setDrivers(List<Driver> drivers) {
        this.drivers = drivers;
    }

    public Driver getSelected() {
        if (current == null) {
            if (this.currentDriverId > -1) {
                this.current = driverFacade.find(this.currentDriverId);
            } else {
                current = new Driver();
            }
        }
        return current;
    }

    public String prepareEdit(Long id) {
        this.current = driverFacade.find(id);
        return "pretty:editDriver";
    }

    public String update() {
        try {
            driverFacade.edit(current);
            JsfUtil.addSuccessMessage("Driver Updated Successfully");
            return "pretty:listDrivers";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareCreate() {
        this.current = new Driver();
        return "pretty:addDriver";
    }

    public String create() {
        try {
            driverFacade.create(current);
            JsfUtil.addSuccessMessage("New Driver Created");
            return "pretty:listDrivers";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy(Long id) {
        try {
            driverFacade.remove(driverFacade.find(id));
            drivers = null;
            JsfUtil.addSuccessMessage("Driver deleted successfully");
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
        }
        return "pretty:listDrivers";
    }
    Long currentDriverId = -1L;

    public Long getCurrentDriverId() {
        return getSelected().getId();
    }

    public void setCurrentDriverId(Long id) {
        this.currentDriverId = id;
    }
}
